package com.cdu.controller;

import com.cdu.commons.R;
import com.cdu.pojo.dto.*;
import com.cdu.pojo.entity.Product;
import com.cdu.pojo.vo.CategoryVO;
import com.cdu.pojo.vo.PageVO;
import com.cdu.pojo.vo.ProductVO;
import com.cdu.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/product")
public class AdminProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("/add")
    public R<Void> addProduct(@Validated @RequestBody ProductAddDTO productAddDTO) {
        productService.addProduct(productAddDTO);
        return R.ok("添加商品成功");
    }

    @PutMapping("/status")
    public R<Void> updateProductStatus(@Validated @RequestBody ProductStatusDTO statusDTO) {
        productService.updateProductStatus(statusDTO);
        return R.ok("更新商品状态成功");
    }

    @GetMapping("/category/list")
    public R<List<CategoryVO>> getCategoryList() {
        List<CategoryVO> categoryList = productService.getCategoryList();
        return R.ok("获取分类列表成功", categoryList);
    }

    @PutMapping("/update")
    public R<Void> updateProduct(@Validated @RequestBody ProductUpdateDTO productUpdateDTO) {
        productService.updateProduct(productUpdateDTO);
        return R.ok("更新商品成功");
    }

    @GetMapping("/{id}")
    public R<Product> getProduct(@PathVariable Integer id) {
        Product product = productService.getProductById(id);
        return R.ok("获取商品成功", product);
    }
}